<template>
  <div class="app-container">

    <u-page title="用户管理">

      <u-filtered>
        <el-form ref="listQuery" :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="卡号" prop="memberId">
            <el-input v-model="listQuery.memberId" placeholder="卡号" clearable />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="listQuery.mobile" placeholder="手机号" clearable maxlength="11" show-word-limit />
          </el-form-item>
          <el-form-item label="OPENID" prop="openid">
            <el-input v-model="listQuery.openid" placeholder="openid" clearable />
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="listQuery.nickname" placeholder="关键词查询" clearable />
          </el-form-item>
          <el-form-item label="同步状态" class="ack">
            <el-select v-model="listQuery.ack" placeholder="同步状态" clearable>
              <el-option label="全部" value="" />
              <el-option label="待同步" value="0" />
              <el-option label="成功" value="1" />
              <el-option label="失败" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-dropdown>
              <el-button type="primary">
                批量操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :disabled="selectData.length===0" @click.native="hdlDel">批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-refresh-right" @click="hdlRetry">一键重传</el-button>
          </el-form-item>

        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button type="text" @click="hdlDelete(row.id, row.nickname)">删除</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <u-dialog :title="textMap[dialogStatus]" :show.sync="dialogFormVisible" @confirm="dialogStatus==='create'?hdlCreate():hdlUpdate()">
      <el-form ref="dialogForm" :rules="rules" :model="dataForm" label-width="80px">

        <el-form-item prop="avatar" label="头像">
          <u-upload v-model="dataForm.avatar" name="file" disabled />
        </el-form-item>

        <el-form-item prop="nickname" label="昵称">
          <el-input v-model="dataForm.nickname" placeholder="用户昵称" clearable maxlength="30" show-word-limit />
        </el-form-item>

        <el-form-item prop="memberId" label="卡号">
          <el-input v-model="dataForm.memberId" placeholder="卡号" clearable disabled />
        </el-form-item>

        <el-form-item prop="mobile" label="手机号">
          <el-input v-model="dataForm.mobile" placeholder="手机号" clearable disabled />
        </el-form-item>

        <el-form-item prop="openid" label="OPENID">
          <el-input v-model="dataForm.openid" placeholder="openid" clearable disabled />
        </el-form-item>

        <el-form-item prop="sex" label="性别">
          <el-radio-group v-model="dataForm.sex">
            <el-radio-button :label="0">男</el-radio-button>
            <el-radio-button :label="1">女</el-radio-button>
            <el-radio-button :label="2">保密</el-radio-button>
          </el-radio-group>
        </el-form-item>

      </el-form>
    </u-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsInteractiveMember,
      // 表格
      columns: [
        {
          prop: 'avatar',
          label: '头像',
          image: true
        },
        {
          prop: 'nickname',
          label: '昵称'
        },
        {
          prop: 'memberId',
          label: '卡号'
        },
        {
          prop: 'mobile',
          label: '手机号'
        },
        {
          prop: 'openid',
          label: 'OPENID'
        },
        {
          prop: 'sex',
          label: '性别',
          render: (h, params) => {
            const { sex } = params.row
            let title = ''
            if (sex === 0) {
              title = '男'
            } else if (sex === 1) {
              title = '女'
            } else if (sex === 2) {
              title = '保密'
            }
            return h('el-tag', { props: { type: 'success' }}, title)
          }
        },
        {
          prop: 'createdAt',
          label: '参加时间',
          timestamp: true
        },
        {
          prop: 'ack',
          label: '同步状态',
          render: (h, params) => {
            if (params.row.ack === 1) {
              return h('el-tag', { props: { type: 'success' }}, '成功')
            } else if (params.row.ack === 2) {
              return h('el-tag', { props: { type: 'danger' }}, '失败')
            } else {
              return h('el-tag', { props: { type: 'warning' }}, '待同步')
            }
          }
        },
        {
          prop: 'note',
          label: '备注'
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        title: '',
        status: ''
      },

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑用户',
        create: '新增用户'
      },
      dataForm: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: ['blur'] }
        ]
      },
      // 删除选中数据
      selectData: [],
      newForm: {
        status: true
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    // 一键重试
    hdlRetry() {
      const self = this
      self.$confirm('确定进行一键重传?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.api.retry().then(() => {
          // 刷新数据列表
          self.hdlList()
          self.$u.msg('操作成功,请稍后查看')
        })
      }).catch(() => {
      })
    },
    // 导出数据
    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'createdAt', item.createdAt ? this.$u.timeFormat(item.createdAt) : '')
        this.$set(item, 'sex', item.sex === 0 ? '男' : item.sex === 1 ? '女' : item.sex === 2 ? '保密' : '')
        this.$set(item, 'ack', item.ack === 0 ? '待同步' : item.ack === 1 ? '成功' : item.ack === 2 ? '失败' : '')
      })

      // 表头&表数据
      // 表头&表数据
      const tHeader = ['头像', '昵称', '卡号', '手机号', 'OPENID', '性别', '参加时间', '备注', '同步状态']
      const filterVal = ['avatar', 'nickname', 'memberId', 'mobile', 'openid', 'sex', 'createdAt', 'note', 'ack']
      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('用户管理', tHeader, data)
    }
  }
}
</script>
